package com.example.administrator.szb.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.GWCententActivity;
import com.example.administrator.szb.bean.GZBean;
import com.example.administrator.szb.bean.WTXQBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;
import java.util.List;

/**
 * Created by stone on 2018/1/18.
 */

public class WTContentAdapter extends android.widget.BaseAdapter {
    private List list;
    private Context context;
    private LayoutInflater inflater;

    public WTContentAdapter(Context context, List list) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public WTXQBean.DataBean.RefersBean getItem(int position) {
        return (WTXQBean.DataBean.RefersBean) list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.view_wtxq_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final WTXQBean.DataBean.RefersBean refersBean = getItem(position);
        if (refersBean.getHeadimg() != null) {
            Glide.with(context).load(refersBean.getHeadimg()).error(R.mipmap.default_header_s).into(((ImageView) holder.getView(R.id.imageView19)));
        }

        ((ImageView) holder.getView(R.id.imageView19)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, GWCententActivity.class);
                intent.putExtra("refer_id", refersBean.getQuiz_id());
                context.startActivity(intent);
            }
        });

        ((TextView) holder.getView(R.id.textView40)).setText(refersBean.getName());




        ((TextView) holder.getView(R.id.textView43)).setText(refersBean.getContent());
        int num = refersBean.getAnswer_count();
        ((TextView) holder.getView(R.id.textView44)).setText(refersBean.getAnswer_count() + "评论");

        return convertView;
    }

    private void doGZOrNOt(int id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        //int id = SampleApplicationLike.getUserloginInstance().getId();
        map.put("id", SampleApplicationLike.getUserloginInstance().getId() + "");
        map.put("quiz_id", id + "");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, GZBean.class, URLAddress.URLBASE + URLAddress.URL_GZ, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                GZBean gzBean = (GZBean) response;
                if (gzBean.getResult() == 1) {
                    Toasts.show(context, "" + gzBean.getData().getMsg(), Toast.LENGTH_SHORT);
                } else {
                    Toasts.show(context, "" + gzBean.getErr_msg(), Toast.LENGTH_SHORT);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
                //Toasts.show(context, ""+error.toString(), Toast.LENGTH_SHORT);
            }
        });
    }

    class ViewHolder {
        View view;

        public ViewHolder(View view) {
            this.view = view;
        }

        public View getView(int id) {
            return view.findViewById(id);
        }
    }
}